﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class emailInfo : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Email information";
            if (!IsPostBack)
            {

                var email = dc.Settings.SingleOrDefault(z => z.type.Equals("Email") && z.name.Equals("Email"));
                TextBox1.Text = email.value.ToString();
                HiddenField1.Value = email.id.ToString();

                var password = dc.Settings.SingleOrDefault(z => z.type.Equals("Email") && z.name.Equals("Password"));
                TextBox2.Text = password.value.ToString();
                HiddenField2.Value = password.id.ToString();

                var smtp = dc.Settings.SingleOrDefault(z => z.type.Equals("Email") && z.name.Equals("Smtp"));
                TextBox3.Text = smtp.value.ToString();
                HiddenField3.Value = smtp.id.ToString();

                var port = dc.Settings.SingleOrDefault(z=>z.type.Equals("Email") && z.name.Equals("Port"));
                TextBox4.Text = port.value.ToString();
                HiddenField4.Value = port.id.ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int id = Convert.ToInt32(HiddenField1.Value);
                var email = dc.Settings.SingleOrDefault(z=>z.id==id);
                email.value = TextBox1.Text.Trim();
                dc.SubmitChanges();

                int idpass = Convert.ToInt32(HiddenField2.Value);
                var password = dc.Settings.SingleOrDefault(z => z.id == idpass);
                password.value = TextBox2.Text.Trim();
                dc.SubmitChanges();
              

                int idSmtp = Convert.ToInt32(HiddenField3.Value);
                var smtp = dc.Settings.SingleOrDefault(z=>z.id==idSmtp);
                smtp.value = TextBox3.Text.Trim();
                dc.SubmitChanges();

                int idport = Convert.ToInt32(HiddenField4.Value);
                var port = dc.Settings.SingleOrDefault(z => z.id == idport);
                port.value = TextBox4.Text.Trim();
                dc.SubmitChanges();
                Response.Redirect("Index.aspx");
            }
        }
    }
}